package com.eos.orm.c3p0.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.eos.common.tool.CoreKeyContext;
import com.eos.orm.c3p0.dao.AbstractC3p0Dao;
import com.eos.orm.c3p0.dao.DBConnectionManager;
import com.eos.orm.mybatis.model.ViewReceiptList;
import com.hf.utils.StringUtil;

public class ViewReceiptListDaoImpl extends AbstractC3p0Dao<ViewReceiptList> {

	/**
	 * 查询的条件：id/deal_flag/status/customer_id/start_time/end_time
	 */
	public String condition1(Map<String, String> map) {
		StringBuilder condition = new StringBuilder();
		if (null == map)
			return condition.append("1=0").toString();
		if (!StringUtil.isEmpty(map.get("id")))
			condition.append("id='").append(map.get("id")).append("' and ");
		if (!StringUtil.isEmpty(map.get("status")))
			condition.append("status='").append(map.get("status"))
					.append("' and ");
		if (!StringUtil.isEmpty(map.get("customer_id")))
			condition.append("customer_id='").append(map.get("customer_id"))
					.append("' and ");
		if (!StringUtil.isEmpty(map.get("product_id")))
			condition.append("product_id='").append(map.get("product_id"))
					.append("' and ");
		if (!StringUtil.isEmpty(map.get("brand_id")))
			condition.append("brand_id='").append(map.get("brand_id"))
					.append("' and ");
		if (!StringUtil.isEmpty(map.get("model_id")))
			condition.append("model_id='").append(map.get("model_id")).append(
					"' and ");
		if (!StringUtil.isEmpty(map.get("receipt_time")))
			condition.append("receipt_time='").append(map.get("receipt_time")).append(
					"' and ");
		if (!StringUtil.isEmpty(map.get("bar_code")))
			condition.append("bar_code like '").append(map.get("bar_code"))
					.append("%' and ");
		return condition.append("1=1").toString();
	}

	@Override
	public List<ViewReceiptList> findPage(Map<String, String> map) {
		List<ViewReceiptList> templist = new ArrayList<ViewReceiptList>();
		if (null == map)
			return templist;
		Connection connection = DBConnectionManager.getConnection();
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		String sql = StringUtil.replace(StringUtil.replace(StringUtil.replace(
				CoreKeyContext.getCoreKey("tabs_page_sqlscript"),
				"$table_name$", map.get("table_name")), "$condition1$",
				condition1(map)), "$condition2$", condition2(map));
		try {
			pstmt = connection.prepareStatement(sql);
			rs = pstmt.executeQuery();
			while (rs.next()) {
				ViewReceiptList temp = new ViewReceiptList();
				temp.setActivtyId(rs.getInt("activty_id"));
				temp.setActivtySonId(rs.getInt("activty_son_id"));
				temp.setBarCode(rs.getString("bar_code"));
				temp.setBrandId(rs.getInt("brand_id"));
				temp.setBrandName(rs.getString("brand_name"));
				temp.setCaseNumber(rs.getString("case_number"));
				temp.setColourId(rs.getInt("colour_id"));
				temp.setColourName(rs.getString("colour_name"));
				temp.setCustomerId(rs.getInt("customer_id"));
				temp.setReceiptDesc(rs.getString("receipt_desc"));
				temp.setReceiptTime(rs.getString("receipt_time"));
				temp.setId(rs.getInt("id"));
				temp.setInsertTime(rs.getString("insert_time"));
				temp.setInsertUserId(rs.getString("insert_user_id"));
				temp.setModelDesc(rs.getString("model_desc"));
				temp.setModelId(rs.getInt("model_id"));
				temp.setModelName(rs.getString("model_name"));
				temp.setProductId(rs.getInt("product_id"));
				temp.setStatus(rs.getString("status"));
				templist.add(temp);
				temp = null;
			}
		} catch (SQLException e) {
			System.out
					.println("SysActivtyDaoImpl exec findPage operator is fail,Exception is :"
							+ e.getMessage());
		} finally {
			freeResource(connection, pstmt, rs);
		}
		return templist;
	}
	
	public List<ViewReceiptList> findByWhere(Map<String, String> map) {
		List<ViewReceiptList> templist = new ArrayList<ViewReceiptList>();
		if (null == map)
			return templist;
		Connection connection = DBConnectionManager.getConnection();
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		String sql = StringUtil.replace(StringUtil.replace(StringUtil.replace(
				CoreKeyContext.getCoreKey("tabs_page_sqlscript"),
				"$table_name$", map.get("table_name")), "$condition1$",
				condition1(map)), "$condition2$", condition2(map));
		try {
			pstmt = connection.prepareStatement(sql);
			rs = pstmt.executeQuery();
			while (rs.next()) {
				ViewReceiptList temp = new ViewReceiptList();
				temp.setActivtyId(rs.getInt("activty_id"));
				temp.setActivtySonId(rs.getInt("activty_son_id"));
				temp.setBarCode(rs.getString("bar_code"));
				temp.setBrandId(rs.getInt("brand_id"));
				temp.setBrandName(rs.getString("brand_name"));
				temp.setCaseNumber(rs.getString("case_number"));
				temp.setColourId(rs.getInt("colour_id"));
				temp.setColourName(rs.getString("colour_name"));
				temp.setCustomerId(rs.getInt("customer_id"));
				temp.setReceiptDesc(rs.getString("receipt_desc"));
				temp.setReceiptTime(rs.getString("receipt_time"));
				temp.setId(rs.getInt("id"));
				temp.setInsertTime(rs.getString("insert_time"));
				temp.setInsertUserId(rs.getString("insert_user_id"));
				temp.setModelDesc(rs.getString("model_desc"));
				temp.setModelId(rs.getInt("model_id"));
				temp.setModelName(rs.getString("model_name"));
				temp.setProductId(rs.getInt("product_id"));
				temp.setStatus(rs.getString("status"));
				templist.add(temp);
				temp = null;
			}
		} catch (SQLException e) {
			System.out
					.println("ViewReceiptListDaoImpl exec findPage operator is fail,Exception is :"
							+ e.getMessage());
		} finally {
			freeResource(connection, pstmt, rs);
		}
		return templist;
	}
}
